# -*- coding: utf-8 -*-

import base
import json,uuid,time,traceback,datetime
import requests
import pika,logging
from main.settings import REDIS_STORE_URL,RABBITMQ_HOST_URL,RABBITMQ_USER,RABBITMQ_USER_PASSWORD,RABBITMQ_PORT

#企业公众号消息
class MqServer(object):
    """
    构造请求数据
    """
    def __init__(self,task_id,area_id):
        self.user=RABBITMQ_USER
        self.pwd=RABBITMQ_USER_PASSWORD
        self.host=RABBITMQ_HOST_URL
        self.port=RABBITMQ_PORT
        self.virtual_host='/'
        self.exchange = 'rb-topic'
        self.routing_key = 'message.qywechat'#不同的主题，使用不同的key
        self.queue='message-qywechat'#不同的消息类型，使用不同的队列
        self.task_id = task_id
        self.area_id = area_id
    
    #接收消息
    def start(self):
        try:
            # self.linshi_que()
            self.durable_que()
            
        except:
            traceback.print_exc()
    #持久化rabbitmq
    def durable_que(self):
        credentials = pika.PlainCredentials(self.user, self.pwd)  # mq用户名和密码
        # 虚拟队列需要指定参数 virtual_host，如果是默认的可以不填。
        connection = pika.BlockingConnection(pika.ConnectionParameters(host = self.host,port = self.port,virtual_host = self.virtual_host,credentials = credentials))
        channel=connection.channel()
        # 声明消息队列，消息将在这个队列传递，如不存在，则创建.
        # queue 声明持久化
        # durable = True 代表消息队列持久化存储，False 非持久化存储
        result = channel.queue_declare(queue = self.queue,durable = True)
        

        currtime = datetime.datetime.now()
        currtime = currtime.strftime("%Y-%m-%d %H:%M:%S")

        projectjson = {
            'project_id':10,
            'corp_id':'wxca4a1d269b62a5cc',
            'secret':'IsX1GrNbOFDvAiBnylneM2LEFYlu-WTIFXaSRccW3ck',
            'redis_url':REDIS_STORE_URL,#动态设置
            'callback_url':None,#回调地址
            'callback_param':{},#回调参数
        }
        msgtype = {
            'code':'qywechat',
            'type':'news',
            'title':'图文消息',
            'agent_id':'1000005',
            'toparty':'',
            'toparty':'',
        }

        #图文消息
        messagejson = {"articles": [
                {
                    "url": "http://yanshi1.cocc.bimsheng.com/#/skipDispose/837/post/true", 
                    "image": "http://yanshi1.cocc.bimsheng.com/img/wxnotice/issue.jpg", 
                    "description": "-设备安装-测试消息发送222", 
                    "title": "总包顾问(王俊)发起质量事宜ZL-2001211026-836"}
            ]
        }
        
        touser = 'WangJun'
        message = json.dumps({'projectjson':projectjson,'createtime':currtime,'messagejson':messagejson,'touser':touser,'msgtype':msgtype})
        print(message)
        # logging.info('------msg----')
        # logging.info(message)
        # 声明exchange，由exchange指定消息在哪个队列传递，如不存在，则创建。durable = True 代表exchange持久化存储，False 非持久化存储
        channel.exchange_declare(exchange = self.exchange,durable = True, exchange_type='topic')
        # channel.basic_publish(exchange = self.exchange,routing_key = self.routing_key,body = message,properties=pika.BasicProperties(delivery_mode = 2))
        channel.basic_publish(exchange = self.exchange,routing_key = self.routing_key,body = message)
        connection.close()

    
if __name__ == "__main__":
    
    mqc = MqServer(1,3)
    mqc.start()



    

    
